<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->   
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
System.out.println("car_name:"+request.getParameter("car_name"));
System.out.println("car_type:"+request.getParameter("car_type"));
Connection conn=null;
try{
	Class.forName("org.h2.Driver");//加载驱动
} catch (ClassNotFoundException e) {
	e.printStackTrace();
	return;
}
try {
	conn = DriverManager.getConnection("jdbc:h2:d:/CompanyCar/temp/mydbtest", "sa", "");
	String sql="insert into car_info(car_name,car_type,license_plate,displacement,start_date,management_department,responsible_person,register_person,asset_number,purchase_date,value,frame_number,car_usage,usage_status,usage_department,usage_person,entry_date) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
	PreparedStatement pstmt  = conn.prepareStatement(sql);
	pstmt.setString(1,request.getParameter("car_name"));
	pstmt.setString(2,request.getParameter("car_type"));
	pstmt.setString(3,request.getParameter("license_plate"));
	System.out.println(request.getParameter("displacement"));
	pstmt.setFloat(4,Float.parseFloat(request.getParameter("displacement")));
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	java.util.Date date=null;
	String dateString=request.getParameter("start_date");
	date= dateFormat.parse(dateString);
	java.sql.Date start_date = new java.sql.Date(date.getTime());
	pstmt.setDate(5,start_date);
	pstmt.setString(6,request.getParameter("management_department"));
	pstmt.setString(7,request.getParameter("responsible_person"));
	pstmt.setString(8,request.getParameter("register_person"));
	pstmt.setString(9,request.getParameter("asset_number"));
	dateString = request.getParameter("purchase_date");
	date = dateFormat.parse(dateString);
	java.sql.Date purchase_date = new java.sql.Date(date.getTime());
	pstmt.setDate(10,purchase_date);
	pstmt.setFloat(11,Float.parseFloat(request.getParameter("value")));
	pstmt.setString(12,request.getParameter("frame_number"));
	pstmt.setString(13,request.getParameter("car_usage"));
	pstmt.setString(14,request.getParameter("usage_status"));
	pstmt.setString(15,request.getParameter("usage_department"));
	pstmt.setString(16,request.getParameter("usage_person"));
	dateString = request.getParameter("entry_date");
	date = dateFormat.parse(dateString);
	java.sql.Date entry_date = new java.sql.Date(date.getTime());
	pstmt.setDate(17,entry_date);
	/*
	Part part = request.getPart("data_attachment");
	if (part != null && part.getSize() > 0) {
		InputStream inputStream = part.getInputStream();
		byte[] bytes = inputStream.readAllBytes();
		Blob data = conn.createBlob();
		data.setBytes(1, bytes);//createBlob()方法需要传入一个InputStream类型的参数，而不是byte[]类型的参数。您可以使用setBytes()方法将字节数组存储到Blob对象中，然后再将Blob对象存储到数据库中。
		pstmt.setBlob(18,data);
	}*/
	System.out.println(sql);
	//执行数据库插入，获取结果集
	int result=pstmt.executeUpdate();
	System.out.println(result);
	//关闭表达式对象
	pstmt.close();			
	//关闭数据库				
	conn.close();
} catch (SQLException e) {
	e.printStackTrace();
} finally{
//防止数据库未正常关闭
try{
	if (conn!=null && (!conn.isClosed())){
		conn.close();
	}
}catch(SQLException e){
	e.printStackTrace();
}
}
response.sendRedirect("C-Car_Admin.jsp");
%>


</body>
</html>